package lib

import (
	"context"
	"gorm.io/gorm"
	"honeyweb/log"
)

//调试信息，检查是否连接数据库成功
func showInfo(tempDB *gorm.DB) {
	name := showDBName(tempDB)
	version := showVersion(tempDB)
	log.S().Debugf("db version: %s", version)

	tbs := showTables(tempDB)
	log.S().Debugf("%s has tables: %v", name, tbs)
}

//展示db的数据版本
func showDBName(tempDB *gorm.DB) string {
	var name string
	tempDB.ConnPool.QueryRowContext(context.TODO(), "SELECT database()").Scan(&name)
	return name
}

//展示db的数据版本
func showVersion(tempDB *gorm.DB) string {
	var version string
	tempDB.ConnPool.QueryRowContext(context.TODO(), "SELECT VERSION()").Scan(&version)
	return version
}

//展示db的数据表
func showTables(tempDB *gorm.DB) []string {
	var tbs []string
	rows, err := tempDB.ConnPool.QueryContext(context.TODO(), "show tables")
	if err == nil {
		for rows.Next() {
			var tb string
			rows.Scan(&tb)
			tbs = append(tbs, tb)
		}
	}
	return tbs
}
